/**
 * Created by caizepeng on 17/4/5.
 */
import Vue from 'vue'
import Router from 'vue-router'
import router from '../../router'
import App from '../../App'
import axios from 'axios'
import Qs from 'qs'
export default class Api {
	UrlsMap = {}
	url = {
		login: 'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		index: 'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		notice: 'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		deployment: 'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		study: 'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		studyData: 'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		ranking: 'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		runThePartyStrictly:'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		flowPartyMember:'http://120.77.203.170:8033/API/VIEWAPI.ashx',
		pushInfoList:'http://120.77.203.170:8033/API/VIEWAPI.ashx',
	}
	constructor() {}
	handerSuccess(notNorm, data, resolve, reject) {
		if(notNorm) {
			resolve(data)
			return
		}
		if(data && data.StateCode === 1) {
			resolve(data)
		} else {
			reject(data)
		}
	}
	cancelRequest(url) {
		if(cancelMap[this.UrlsMap[url]]) {
			cancelMap[this.UrlsMap[url]]()
		}
	}
	get(url, requestData){
		return new Promise(function(resolve, reject) {
			axios.get(url,requestData).then(function(result) {
				console.log(result);
				if(result.data.ErrorMsg == 'WXTIMEOUT'){
					alert('因为您长时间不在线，系统为了您的账户安全将自动退出您的账户，请重新登录！');
					localStorage.removeItem("result");
					localStorage.removeItem("userRealName");
					localStorage.removeItem('checked');
					router.push('/');
				}
				resolve(result)
			}).catch(function(err){ 
				console.log(err) 
				reject(err)
			})
		})
	}
	post(url, requestData) {
		return this.request(url, requestData, false, 'post')
	}
	//getUserData() {
	//	return token
	//}
	request(url, requestData, notNorm, methods) {
		var self = this
		methods = methods ? methods : 'post'
		//  var token = this.getUserData();
		//  if(token) {
		//  	requestData.token = token;
		//  }
		return new Promise(function(resolve, reject) {
			axios({
					method: methods,
					url: url,
					data: Qs.stringify(requestData)
			}).then(function(response) {
				if(response.data.ErrorMsg == 'WXTIMEOUT'){
					alert('因为您长时间不在线，系统为了您的账户安全将自动退出您的账户，请重新登录！');
					router.push('/');
					localStorage.removeItem("result");
					localStorage.removeItem("userRealName");
					localStorage.removeItem('checked');
				}
				console.log('request success ');
				console.log(response)
				resolve(response)
			})
			.catch(function(error) {
				console.log(error)
				reject(error)
			})
		})
	}
	//信息详情中的图片src格式转换第1次
	firstExchange(val){
		return val.replace(/DownFile.aspx\?/g,'DownFile.aspx');
	}
	//信息详情中的图片src格式转换第2次
	secondExchange(val){
		var result = localStorage.getItem('result');
		var reg = new RegExp("/ViewV5/Base/DownFile.aspx","g");
		return val.replace(reg,'http://120.77.203.170:8033/ViewV5/Base/DownFile.aspx?szhlcode=' + result + "&");
	}
	
}

new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})